package club.wx3609.images.controller;

import club.wx3609.images.Utils.IsNullUtils;
import club.wx3609.images.entity.Friend;
import club.wx3609.images.entity.User;
import club.wx3609.images.interfaces.Constant;
import club.wx3609.images.service.FriendService;
import club.wx3609.images.service.UserService;
import com.alibaba.fastjson.JSON;
import com.wx3609_zlj.utils.CharacterProcessing.DivisionString;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@RestController
public class FriendController {

    @Resource
    private FriendService friendService;
    @Resource
    private UserService userService;

    @Transactional
    @PostMapping("/addFriend")
    public String addFriend(String ReqPerson,String ResPerson){
        String resPerson= DivisionString.spotBehind(ResPerson,':');
        String reqPerson=DivisionString.spotBehind(ReqPerson,':');
        if(IsNullUtils.character(reqPerson)&&IsNullUtils.character(resPerson)){
            User user=userService.registerVerification(resPerson);
            if(user!=null) {
                List<Friend> list=friendService.findByResPerson(resPerson);
                for(Friend friend1:list){
                    if(friend1.getReqPerson().equals(reqPerson.trim())){
                        System.out.println(friend1.getAddFriendState());
                        if (friend1.getAddFriendState().equals(Constant.addFriendState1)) {
                            return "已申请添加，请勿重复添加";
                        } else if(friend1.getAddFriendState().equals(Constant.addFriendState2)){
                            return "你们已经是好友了";
                        }
                    }
                }
                Friend friend = new Friend();
                friend.setReqPerson(reqPerson);
                friend.setResPerson(resPerson);
                friend.setAddFriendState(Constant.addFriendState1);
                friendService.addOne(friend);
                return Constant.addFriendState1;
            }
            return Constant.addFriendErrorState0;
        }
        return Constant.inputJudge0;
    }

    @Transactional
    @PostMapping("/delFriend")
    public String delFriend(String reqPerson,String ResPerson){
        if(IsNullUtils.character(reqPerson)&&IsNullUtils.character(ResPerson)){
            Friend friend=null;
            List<Friend> list=friendService.findByReqPerson(ResPerson);
            for(Friend friend1:list){
                if(friend1.getReqPerson().equals(reqPerson)){
                    friend=friend1;
                    break;
                }
            }
            if(friend!=null){
                friend.setAddFriendState(Constant.addFriendState0);
                friendService.save(friend);
                Friend friend2=null;
                List<Friend> list2=friendService.findByReqPerson(reqPerson);
                for(Friend friend3:list2){
                    if(friend3.getResPerson().equals(ResPerson)){
                        friend2=friend3;
                        break;
                    }
                }
                friend2.setAddFriendState(Constant.addFriendState0);
                friendService.save(friend2);
                return Constant.addFriendState0;
            }
            return Constant.addFriendErrorState0;
        }
        return Constant.inputJudge0;
    }

    @PostMapping("/findFriendOne")
    public String findFriendOne(String friendNumber){
        if(IsNullUtils.character(friendNumber)) {
            User user = userService.registerVerification(friendNumber);
            if (user != null) {
                return JSON.toJSONString(user);
            }
        }
        return Constant.addFriendErrorState0;
    }

    @PostMapping("/findByFriend")
    public String findByFriend(String ReqPerson){
        if(IsNullUtils.character(ReqPerson)){
            List<Friend> list=friendService.findByResPerson(ReqPerson);
            List<Friend> list0=new ArrayList<>();
            if(list.size()>0){
                for(Friend friend:list){
                    if(friend.getAddFriendState().equals(Constant.addFriendState1)){
                        list0.add(friend);
                    }
                }
                if(list0.size()>0) {
                    return JSON.toJSONString(list0);
                }
            }
            return Constant.message0;
        }
        return Constant.inputJudge0;
    }


    @PostMapping("/getMyFriends")
    public String getMyFriends(String ReqPerson){
        if(IsNullUtils.character(ReqPerson)){
            List<Friend> list=friendService.findByReqPerson(ReqPerson);
            List<User> list0=new ArrayList<User>();
            if(list.size()>0){
                for(Friend friend:list){
                    if(friend.getAddFriendState().equals(Constant.addFriendState2)){
                        User user=userService.registerVerification(friend.getResPerson());
                        list0.add(user);
                    }
                }
                return JSON.toJSONString(list0);
            }
        }
        return Constant.myFriends0;
    }

    @Transactional
    @PostMapping("/IsFriend")
    public String IsFriend(String resPerson,String reqPerson,String Boolean){
        if(IsNullUtils.character(reqPerson)&&IsNullUtils.character(resPerson)&&IsNullUtils.character(Boolean)){
            List<Friend> list0=friendService.findByReqPerson(reqPerson);
            if(Boolean.equals("yes")) {
                for(Friend friend0:list0){
                    if(friend0.getResPerson().equals(resPerson)&&friend0.getAddFriendState().equals(Constant.addFriendState1)){
                        friend0.setAddFriendState(Constant.addFriendState2);
                        friendService.save(friend0);
                        Friend friend1=new Friend();
                        friend1.setAddFriendState(Constant.addFriendState2);
                        friend1.setReqPerson(resPerson);
                        friend1.setResPerson(reqPerson);
                        friendService.save(friend1);
                        return Constant.addFriendState2;
                    }
                }
                return Constant.requestFriend0;
            }else{
                for(Friend friend0:list0) {
                    if (friend0.getResPerson().equals(resPerson) && friend0.getAddFriendState().equals(Constant.addFriendState1)) {
                        friend0.setAddFriendState(Constant.addFriendState3);
                        friendService.save(friend0);
                        return Constant.addFriendState3;
                    }
                }
                return Constant.requestFriend0;
            }
        }
        return Constant.inputJudge0;
    }

}
